#!/bin/sh

EXE_FILE="/usr/bin/gateway"
OTA_FILE="/root/gateway.update"
PID_FILE="/root/pidfile.pid"

start() {

    ## 1  替换OTA 文件
    ## 判断是否存在OTA文件 
    if [ -f $OTA_FILE ]
    then 
        #替换 
        rm -f $EXE_FILE
        mv -f $OTA_FILE  $EXE_FILE
        chmod +x $EXE_FILE
    fi

    ## 2  启动程序 
	start-stop-daemon -S -q -p $PID_FILE \
		--exec $EXE_FILE -- daemon
	[ $? = 0 ] && echo "OK" || echo "FAIL"
	printf "Start  gateway  ."

}
stop() {
	printf "Stopping gateway   "
	start-stop-daemon -K -q -p $PID_FILE 
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
